package com.czy.owner.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.widget.RoundImageView;
import com.easemob.cases.MessageHelper;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.domain.EaseUser;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.List;

/**
 * Created by King on 2016/9/22 0022.
 */
public class GroupDetailsAdapter extends BaseRecyclerAdapter<String, GroupDetailsAdapter.GroupDetailsViewHolder> {
    private int showType = 0; // 0 为 群资料页显示,布局为上头像,下名称, 1 为全部成员页 左头像右名称
    public final static int SHOW_TYPE_DETAILS = 0;
    public final static int SHOW_TYPE_ALL = 1;
    private String groupOwner;

    public GroupDetailsAdapter(Context context, List<String> data, int showType, String groupOwner) {
        super(context, data);
        this.showType = showType;
        this.groupOwner = groupOwner;
    }

    public GroupDetailsAdapter(Context context, List<String> data, boolean slideEnabled, boolean reboundEnabled) {
        super(context, data, slideEnabled, reboundEnabled);
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }

    public String getItem(int position) {
        return data.get(position);
    }

    public boolean isOwner() {
        if (groupOwner == null) {
            return false;
        } else {
            return groupOwner.equals(EMClient.getInstance().getCurrentUser());
        }
    }

    public String getAvatar(int position) {
        EaseUser user = MessageHelper.getInstance().getUserInfo(getItem(position));
        if (user == null) {
            return null;
        } else {
            return user.getAvatar();
        }
    }

    public String getNick(int position) {
        EaseUser user = MessageHelper.getInstance().getUserInfo(getItem(position));
        if (user == null) {
            return null;
        } else {
            return user.getNick();
        }
    }


    @Override
    public void onBindViewHolder(final GroupDetailsViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(position);
    }

    @Override
    public GroupDetailsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (showType == SHOW_TYPE_DETAILS) {
            return new GroupDetailsViewHolder(layoutInflater.inflate(R.layout.item_group_details_recycler, parent, false));
        } else {
            return new GroupDetailsViewHolder(layoutInflater.inflate(R.layout.item_group_details_recycler, parent, false));
        }
    }

    public class GroupDetailsViewHolder extends BaseRecyclerAdapter.ViewHolder {
        private RoundImageView ivAvatar;
        private TextView tvNick;
        private ImageView ivOwner;

        public GroupDetailsViewHolder(View itemView) {
            super(itemView);
            if (showType == SHOW_TYPE_DETAILS) {
                ivAvatar = (RoundImageView) itemView.findViewById(R.id.iv_item_avatar);
                tvNick = (TextView) itemView.findViewById(R.id.tv_item_nick);
                ivOwner = (ImageView) itemView.findViewById(R.id.iv_item_owner);
            }
        }

        public void bindData(int position) {
            if (showType == SHOW_TYPE_DETAILS) {
                ivAvatar.setTag(position);
                String avatarStr = getAvatar(position);
                if (!TextUtils.isEmpty(avatarStr)) {
                    ivAvatar.setTag(avatarStr);
                    x.image().bind(ivAvatar, avatarStr, new Callback.CommonCallback<Drawable>() {
                        @Override
                        public void onSuccess(Drawable result) {

                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            ivAvatar.setImageResource(R.mipmap.icon_connect_portrait);
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {
                            ivAvatar.setImageResource(R.mipmap.icon_connect_portrait);
                        }

                        @Override
                        public void onFinished() {

                        }
                    });
                } else {
                    ivAvatar.setImageResource(R.mipmap.icon_connect_portrait);
                }
                if (!TextUtils.isEmpty(getNick(position))) {
                    tvNick.setText(getNick(position));
                } else {
                    tvNick.setText(getItem(position));
                }
                if (getItem(position).equals(groupOwner)) {
                    ivOwner.setVisibility(View.VISIBLE);
                } else {
                    ivOwner.setVisibility(View.INVISIBLE);
                }
            }


        }
    }


    public interface GroupOperationListener {
        void addMember();

        void removeMember(String userName);
    }
}
